//
//  SetGestureLockViewController.m
//  app
//
//  Created by 余钦 on 16/5/4.
//
//

#import "SetGestureLockViewController.h"
#import "GestureLockView.h"
#import "WYCMePhotoModel.h"


#define tipLabelFontSize 20

#define MarginY 10

#define TextColor [UIColor blueColor]

@interface SetGestureLockViewController ()<GestureLockViewDelegate>


@property(nonatomic, weak)UIImageView *lockViewShotView;
@property(nonatomic, weak)UILabel *tipLabel;
@property(nonatomic, weak)GestureLockView *gestureLockView;
@property(nonatomic, weak)UIButton *resetButton;
@property(nonatomic, copy)NSString *firstLockPath;
@property(nonatomic, copy)NSString *secondLockPath;
/** 模型*/
@property(nonatomic,strong)WYCMePhotoModel * MePhotoModel;
/** 头像*/
@property(nonatomic,strong)UIImageView * photoImageView;
@end

@implementation SetGestureLockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self SetupSubviews];
    self.title = @"设置手势";
    
    [self requster];
}
- (void)requster{
    
    NSString * str = [NSString stringWithFormat:@"%@token=%@",userPortraitDetail,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dict = responseObject[@"result"];
            self.MePhotoModel = [WYCMePhotoModel mj_objectWithKeyValues:dict];
            if (self.MePhotoModel.imgUrl.length == 0){
                self.photoImageView.image = [UIImage imageNamed:@"96"];
                
            }else{
                [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:self.MePhotoModel.imgUrl]];
            }
        }else{
            [self showToast:msgStr];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
}
- (void)SetupSubviews{
    
    
    UIImageView * cellImage = [[UIImageView alloc] init];
    
    cellImage.frame = CGRectMake(ScreenWidth / 2 - 120 * px,64 + 80 * px, 240 * px, 240 * px);
    cellImage.layer.cornerRadius = 120 * px;
    cellImage.layer.masksToBounds = YES;
    [self.view  addSubview:cellImage];
    self.photoImageView = cellImage;
    
    
    
    CGFloat mainViewW = self.view.frame.size.width;
    CGFloat mainViewH = self.view.frame.size.height;
    
    CGFloat smallLockViewW = 100.0f;
    CGFloat smallLockViewH = 100.0f;
    CGFloat smallLockViewX = (mainViewW - smallLockViewW)/2.0f;
    CGFloat smallLockViewY = 64 + 20.0f;
    UIImageView *lockViewShotView = [[UIImageView alloc] init];
    lockViewShotView.frame = CGRectMake(smallLockViewX, smallLockViewY, smallLockViewW, smallLockViewH);
    [self.view addSubview:lockViewShotView];
    self.lockViewShotView = lockViewShotView;
    
    CGFloat tipLabelH = 32.0f;
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.lockViewShotView.frame) + MarginY, mainViewW, tipLabelH)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.text = @"请输入您的手势密码";
    tipLabel.textColor = WYCMain;
    tipLabel.font = [UIFont systemFontOfSize:tipLabelFontSize];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:tipLabel];
    self.tipLabel = tipLabel;
    
    CGFloat lockViewW = self.view.frame.size.width;
    GestureLockView *lockView = [[GestureLockView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tipLabel.frame) + MarginY, lockViewW, lockViewW)];
    lockView.delegate = self;
    [self.view addSubview:lockView];
    self.gestureLockView = lockView;
    
    //    [self GetLockViewShot];
    
    //    CGFloat buttonH = 30;
    //    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    //    [cancelButton setTitleColor:TextColor forState:UIControlStateNormal];
    //    cancelButton.frame = CGRectMake(20, mainViewH - buttonH - 20, 80, buttonH);
    //    [cancelButton addTarget:self action:@selector(ClickCancelButton) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:cancelButton];
    //
    //    UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    resetButton.hidden = YES;
    //    [resetButton setTitle:@"重设" forState:UIControlStateNormal];
    //    [resetButton setTitleColor:TextColor forState:UIControlStateNormal];
    //    resetButton.frame = CGRectMake(mainViewW - 80 - 20, cancelButton.frame.origin.y, 80, buttonH);
    //    [resetButton addTarget:self action:@selector(ClickResetButton) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:resetButton];
    //    self.resetButton = resetButton;
}

//- (void)GetLockViewShot{
//    //截图
//    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.gestureLockView.frame.size.width, self.gestureLockView.frame.size.height), YES, 0);     //设置截屏大小
//    [[self.gestureLockView layer] renderInContext:UIGraphicsGetCurrentContext()];
//    UIImage *shotImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//
//    self.lockViewShotView.image = shotImage;
//}

- (void)ClickCancelButton{
    //    [self dismissViewControllerAnimated:YES completion:nil];
    //    if (_noticBlock) {
    //        _noticBlock(YES);
    //    }
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)ClickResetButton{
    self.firstLockPath = @"";
    self.tipLabel.textColor = WYCMain;
    self.tipLabel.text = @"请输入您的手势密码";
    //    [self GetLockViewShot];
}

#pragma mark ----- GestureLockView delegate
- (void)lockView:(GestureLockView *)lockView BeganTouch:(NSSet *)touchs
{
    self.tipLabel.textColor = WYCMain;
    self.tipLabel.text = @"请再次输入您的手势密码";
}

- (void)lockView:(GestureLockView *)lockView didFinishPath:(NSString *)path shotImage:(UIImage *)shotImage
{
    
    if (self.string == YES) {
        if (path.length < 4) {
            self.tipLabel.textColor = [UIColor redColor];
            self.tipLabel.text = @"请连接至少4个点";
            return ;
        }
        
        if (self.firstLockPath.length) {
            if ([path isEqualToString:self.firstLockPath]) {
                self.tipLabel.textColor = WYCMain;
                self.tipLabel.text = @"手势密码设置成功";
                [self SaveLockPath:path];
                WYCLog(@"-------%@",path);
                [NSThread sleepForTimeInterval:1.2f];
                
                
                [self.navigationController popViewControllerAnimated:YES];
                if (_noticBlock) {
                    _noticBlock(NO);
                    
                }
            }else{
                self.tipLabel.textColor = [UIColor redColor];;
                self.tipLabel.text = @"两次密码输入不一致";
                self.resetButton.hidden = NO;
            }
        }else{
            self.firstLockPath = [path copy];
            //        self.lockViewShotView.image  = shotImage;
        }
    } else {
        NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
        NSString * userlogin =[userDef objectForKey:@"LockPath"];
        if ([path isEqualToString:userlogin]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"yes" object:nil];
        } else {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"no" object:nil];
            self.tipLabel.text = @"手势密码不正确，重新输入";
        }
    }
    
    
    
}

- (void)SaveLockPath:(NSString *)path{
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    [userDef setObject:path forKey:@"LockPath"];
    [userDef synchronize];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    
    NavRigsterLabel.text =self.string ?@"设置手势密码":@"输入密码";
    [NavView addSubview:NavRigsterLabel];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end

